package com.yangtzeu.jwc.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;

import com.yangtzeu.jwc.businessservice.BusinessHttpCallBack;
import com.yangtzeu.jwc.businessservice.impl.StudentServiceImpl;
import com.yangtzeu.jwc.customview.CustomeProgressDialog;
import com.zhangdi.jwc.R;
import com.zjsx.lib.utils.T;

public class ChangePasswordActivity extends BaseActivity implements TextWatcher {

    private EditText pwd1, pwd2;
    private Button cancel, ok;
    //private RadioButton stars1, stars2, stars3, stars4, stars5;
    private LinearLayout ll_stars;
    private ProgressDialog dialog;
    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void setView() {
        setContentView(R.layout.activity_changepassword);
    }

    @Override
    public void initData() {
        super.initData();

    }

    @Override
    public void initActionBar() {
        setSimpleActionBar(null, "修改密码", null, null);
    }

    @Override
    public void initView() {
        super.initView();
        pwd1 = (EditText) findViewById(R.id.pwd1);
        pwd2 = (EditText) findViewById(R.id.pwd2);
        cancel = (Button) findViewById(R.id.cancel);
        ok = (Button) findViewById(R.id.ok);
        //stars1 = (RadioButton) findViewById(R.id.stars_1);
        //stars2 = (RadioButton) findViewById(R.id.stars_2);
        //stars3 = (RadioButton) findViewById(R.id.stars_3);
        //stars4 = (RadioButton) findViewById(R.id.stars_4);
        //stars5 = (RadioButton) findViewById(R.id.stars_5);
        ll_stars = (LinearLayout) findViewById(R.id.ll_stars);
    }

    @Override
    public void initListener() {
        super.initListener();
        cancel.setOnClickListener(this);
        ok.setOnClickListener(this);
        pwd1.addTextChangedListener(this);
    }

    @Override
    public void initAdapter() {
        super.initAdapter();

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.cancel:
                this.finish();
                break;

            case R.id.ok:
                password1 = pwd1.getText().toString().trim();
                password2 = pwd2.getText().toString().trim();
                if (checkPassword(password1, password2)) {
                    changePassword();
                }
                break;
        }
    }

    String password1;
    String password2;

    public void changePassword() {
        dialog = CustomeProgressDialog.show(this, "请稍候。。。");
        dialog.setCancelable(false);
        final StudentServiceImpl ssi = StudentServiceImpl.getInstance();
        ssi.changePassword(password1, new BusinessHttpCallBack() {

            @Override
            public void onFailure(byte[] responseBody, String failuremsg, boolean isNoticeDialog) {
                // TODO Auto-generated method stub
                super.onFailure(responseBody, failuremsg, isNoticeDialog);
                dialog.dismiss();
                T.showLong(ChangePasswordActivity.this, failuremsg);
            }

            @Override
            public void onSuccess(byte[] responseBody, String successMsg, boolean isNoticeDialog, Object obj) {
                // TODO Auto-generated method stub
                dialog.dismiss();
                ssi.modifySavedStudent(ChangePasswordActivity.this, MainActivity.student.getSno(), password1);
                T.showLong(ChangePasswordActivity.this, "密码，修改成功！");
                mHandler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        finish();
                    }
                }, 500);
            }

        });
    }

    public boolean checkPassword(String password1, String password2) {
        if ("".equals(password1)) {
            T.showShort(this, "请输入新密码");
            return false;
        } else if (password1.length() < 5) {
            T.showShort(this, "长度不能小于六位");
            return false;
        } else if (password1.equals("^\\d\\d*\\d$")) {
            T.showShort(this, "不能为纯数字");
            return false;
        } else if ("".equals(password2) || !password1.equals(password2)) {
            T.showShort(this, "两次密码不一致");
            return false;
        }
        return true;
    }

    @Override
    public void afterTextChanged(Editable s) {
        int i = 0;
        String tpwd = pwd1.getText().toString();
        if (pwd1.length() > 5) {
            i++;
            if (tpwd.matches(".*[a-z].*")) {
                i++;
            }
            if (tpwd.matches(".*[A-Z].*")) {
                i++;
            }
            if (tpwd.matches(".*\\W.*")) {
                i++;
            }
            if (tpwd.length() > 7) {
                i++;
            }
        } else {
            i = 0;
        }
        changeStars(i);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    public void changeStars(int i) {
        for (int a = 1; a < i + 1; a++) {
            ((RadioButton) ll_stars.getChildAt(a)).setChecked(true);
        }

        for (int b = i + 1; b < 6; b++) {
            ((RadioButton) ll_stars.getChildAt(b)).setChecked(false);
        }
    }

}
